package com.clkj.tramcarlibrary.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.bumptech.glide.Glide;
import com.clkj.tramcarlibrary.R;

import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;


public class PhotoFragment extends DialogFragment {


    private PhotoView ivPhoto;

    private String photoPath;

    public PhotoFragment() {
        // Required empty public constructor
    }

    public static PhotoFragment newInstance(String path) {
        //创建一个带有参数的Fragment实例
        PhotoFragment fragment = new PhotoFragment();
        Bundle bundle = new Bundle();
        bundle.putString("result", path);
        fragment.setArguments(bundle);//把参数传递给该DialogFragment
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_photo, container, false);
        ivPhoto = (PhotoView) view.findViewById(R.id.iv_photo_detail);
        photoPath = getArguments().getString("result");
        Log.e("路径地址", photoPath);
        // Glide.with(getActivity()).load(photoPath).into(ivPhoto);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Glide.with(getActivity()).load(photoPath).into(ivPhoto);
        ivPhoto.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
            @Override
            public void onPhotoTap(View view, float v, float v1) {
                dismiss();
            }
        });

    }


    @Override
    public void onStart() {
        super.onStart();
        // getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);

        /*
        //设置DialogFragment样式一
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        getDialog().getWindow().setLayout(dm.widthPixels, getDialog().getWindow().getAttributes().height);
        getDialog().getWindow().setGravity(Gravity.BOTTOM);*/
        //设置DialogFragment样式二
        Window win = getDialog().getWindow();
        win.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        win.setAttributes(lp);
        win.setGravity(Gravity.CENTER);
        win.setBackgroundDrawableResource(R.color.transparent);
    }


}
